using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using GameLib.Enums;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;

namespace _15_puzzle_camera_wp8
{
    public class MenuManager
    {
        private SpriteFont _menuFont;
        private SpriteFont _titleFont;
        private Vector2 _menuHighscorePosition;
        private Vector2 _menuAboutPosition;
        private Vector2 _titlePosition;
        private Vector2 _helpPosition;
        private float _helpColor;
        private const string _titleText = "Camera Puzzle";
        private const string _menuHigscoreText = "Highscore";
        private const string _menuAboutText = "About";
        private const string _helpText = "Press camera\nto begin!";

        private Rectangle _touchAreaCamera;

        private SoundEffect _soundSelectMenu;

        private Texture2D _logoTexture;
        private Vector2 _logoPosition;

        private MenuState _menuState;
        private MenuState _menuStatePrevious;

        public event EventHandler GameStart;
        public event EventHandler GameResume;
        public event EventHandler GameQuit;

        private int _msSinceLastMove;
        private int moveSpeed = 2;
        private int _waitUntil = 200;
        private int _waitedTime;
        private bool _showHelpText = false;

        private Texture2D debugBox;

        public MenuManager(ContentManager content)
        {
            //var singleton = Singleton.Instance;

            _menuState = MenuState.Start;
            _menuStatePrevious = MenuState.Start;

            _menuFont = content.Load<SpriteFont>("MenuFont");
            _menuHighscorePosition = new Vector2(20, 400);
            _menuAboutPosition = new Vector2(660, 400);
            _titleFont = content.Load<SpriteFont>("TitleFont");
            _titlePosition = new Vector2(200, 250);
            _logoTexture = content.Load<Texture2D>("logo");
            _logoPosition = new Vector2(220, 50);
            _touchAreaCamera = new Rectangle(200, 40, 400, 200);


            debugBox = content.Load<Texture2D>("selectRectangle");

            _helpPosition = new Vector2(550, 20);
            _helpColor = 0.0f;

            _soundSelectMenu = content.Load<SoundEffect>("selectMenu");
        }

        public void Update(TimeSpan elaspedGameTime)
        {
            _msSinceLastMove += elaspedGameTime.Milliseconds;
            if (_msSinceLastMove > moveSpeed)
            {
                _msSinceLastMove = 0;
                
                if (!_showHelpText)
	            {
		            _waitedTime++;
                    if(_waitedTime > _waitUntil)
                        _showHelpText = true;
	            }
                else
	            {
                    if (_helpColor < 1f)
                        _helpColor = _helpColor + 0.01f;
	            }
            }

            var touches = TouchPanel.GetState();

            foreach (var touch in touches)
            {
                if (touch.State == TouchLocationState.Pressed)
                {
                    
                    Debug.WriteLine("Pressed");
                }
                if (touch.State == TouchLocationState.Moved)
                {
                    Debug.WriteLine("Moved");
                }
                if (touch.State == TouchLocationState.Released)
                {
                    Debug.WriteLine("Released");

                    if (_touchAreaCamera.Intersects(new Rectangle((int)touch.Position.X - 5, (int)touch.Position.Y - 5, 10, 10)))
                    {
                        Debug.WriteLine("User pressed camera!");

                        if (GameStart != null)
                            GameStart(this, new EventArgs());
                    }
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(_logoTexture, _logoPosition, Color.White);
            spriteBatch.DrawString(_titleFont, _titleText, _titlePosition, Color.Black);
            spriteBatch.DrawString(_menuFont, _menuHigscoreText, _menuHighscorePosition, Color.Black);
            spriteBatch.DrawString(_menuFont, _menuAboutText, _menuAboutPosition, Color.Black);

            spriteBatch.DrawString(_menuFont, _helpText, _helpPosition, Color.Black * _helpColor);

            //spriteBatch.Draw(debugBox, _touchAreaCamera, Color.White);
        }
    }
}
